package com.zscg.sequence.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.zscg.sequence.R;

public class BaseListView extends ListView {

	LoadMoreView loadMoreView;
	
	BaseListAdapter baseAdapter;

	public BaseListView(Context context, AttributeSet attrs) {
		super(context, attrs);

		loadMoreView = (LoadMoreView) inflate(context, R.layout.loadmore_view,
				null);
		if (loadMoreView != null) {
			this.addFooterView(loadMoreView);
		}
	}

    public void setAdapter(ListAdapter adapter) {
    	baseAdapter = (BaseListAdapter)adapter;
    	baseAdapter.listView = this;
    	super.setAdapter(adapter);
    }
	
	public LoadMoreView getLoadMoreView() {
		return loadMoreView;
	}

	public void loadMore() {
		if (loadMoreView != null) {
			loadMoreView.loading();
		}
		try {
			baseAdapter.baseLoadMore();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean performItemClick(View view, int position, long id) {
		if (loadMoreView != null && view == loadMoreView
				&& loadMoreView.isReady()) {
			loadMore();
			return true;
		}
		return super.performItemClick(view, position, id);
	}
	
	public void nomore() {
		this.removeFooterView(loadMoreView);
	}
}
